<template>
  <div class="pending">
    <!-- 搜索 -->
    <el-form :inline="true" :model="query">
      <el-form-item label="方案名称">
        <el-input
          v-model="query.logisticsName"
          maxlength="20"
          placeholder="请输入方案名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="search">查询</el-button>
        <el-button type="success" plain @click="handleCreate">新增物流方案</el-button>
        <template v-if="btnList && btnList.length">
          <span
            v-for="(item, index) in btnList"
            :key="index"
            class="promissStyle"
          >
            <el-button type="success" plain @click="btnClick(item)">{{
              item.permissionName
            }}</el-button>
          </span>
        </template>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
      tooltip-effect="dark"
      style="width: 100%"
      class="dataTable"
    >
      <el-table-column label="方案名称" width="220">
        <template #default="scope">{{
          scope.row.logisticsName
        }}</template>
      </el-table-column>
      <el-table-column prop="regions" label="可配送范围" width="420" />
      <el-table-column prop="name" label="计费方式" width="220">
        <template #default="scope">
          <span v-if="scope.row.chargeType == 1">按件数</span>
          <span v-if="scope.row.chargeType == 2">按重量</span>
          <span v-if="scope.row.chargeType == 3">包邮</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip>
        <template #default="scope">
          <div class="btnList">
            <el-button type="primary" link @click="seeMore(scope.row)">查看</el-button>
            <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm
              title="确认删除？"
              @confirm="handleDelete(scope.row)"
            >
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      :total="page.total"
      style="margin-top: 20px;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 方案组件 -->
    <grope-edit
      ref="editRef"
      :type="dialog.type"
      @success="getLists"
    />
  </div>
</template>

<script setup>
import { defineComponent, onMounted, reactive, ref } from 'vue'

import { getBtnList } from '@/utils/auth'
import { logisticsGetAll, logisticsDelete } from '@/api/shopSys'
import GropeEdit from './Edit.vue'

defineComponent({
  name: 'LogisticsPage'
})

/** 权限按钮 */
let btnList = ref([]);

let page = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})
let query = reactive({
  logisticsName: ''
})
let tableData = ref([])
// 初始化查询所有数据
function getList () {
  logisticsGetAll(Object.assign({}, page, query)).then(res => {
    tableData.value = res.data.list
    page.total = res.data.total
  })
}
function handleCurrentChange (val) {
  page.page = val
  getList()
}
function handleSizeChange (val) {
  page.pageSize = val
  getList()
}

function getLists () {
  getList()
}
// 查询
function search () {
  page.page = 1
  getList()
}

function btnClick (id) {
  if (id.permissionName === '新建方案') {
    handleCreate()
  }
}

let dialog = reactive({
  type: 'add',
  isVisible: false,
})

// 新建方案
function handleCreate () {
  dialog.type = 'add'
  dialog.isVisible = true
  openForm()
}
// 查看
function seeMore (row) {
  const { logisticsId } = row
  dialog.type = 'check'
  dialog.isVisible = true

  openForm({ checkId: logisticsId, })
}
// 编辑
function handleEdit (row) {
  const { logisticsId } = row
  dialog.type = 'edit'
  openForm({ checkId: logisticsId, })
}
// 删除
function handleDelete (row) {
  ElMessageBox.confirm('选中数据将被永久删除, 是否继续？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      logisticsDelete({ logisticsId: row.logisticsId }).then((res) => {
        if (res.code === '') {
          ElMessage.success('删除成功')
        }
        getList()
      })
    })
    .catch(() => {})
}

/** 表单组件实例 */
const editRef = ref();
function openForm (data) {
  editRef.value.open(data)
}

onMounted(() => {
  getList()
  btnList.value = getBtnList() || []
})

</script>

<style lang="scss" scoped>
.pending {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
}
</style>
